package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import object.UserCenter;
import object.ZcAdmin;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import pojo.ZcUser;
import pojo.ZcUserAddr;
import utils.MD5Helper;
import config.Sql2zcConfig;
import config.SqlConfig;


@Repository
public class User2zcDao {
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	static Logger logger = Logger.getLogger(User2zcDao.class.getName());
	
	/**
	 * 根据微信标识获取用户信息
	 * @param wxID
	 * @return
	 */
	public UserCenter adminLogin(String loginName,String loginPsd) {
		final UserCenter user = new UserCenter();
		Object[] params = new Object[]{ loginName,MD5Helper.getMD5Str(loginPsd) };
		jdbcTemplate.query(Sql2zcConfig.adminLogin, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				user.setUserId(rs.getInt("user_id"));
				user.setFirstName(rs.getString("user_firstName"));
				user.setLastName(rs.getString("user_firstName"));
				user.setMobileNum(rs.getString("user_mobileNum"));
			}
		});
		if(user.getUserId()!=null&&user.getUserId() != -1)
			return user;
		return null;
	}
	
	/**
	 * 根据微信标识获取用户信息
	 * @param wxID
	 * @return
	 */
	public ZcAdmin getZcAdmin(String loginName) {
		final ZcAdmin admin = new ZcAdmin();
		Object[] params = new Object[]{ loginName };
		jdbcTemplate.query(Sql2zcConfig.queryZcAdmin, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				admin.setUserId(rs.getInt("user_id"));
				admin.setFirstName(rs.getString("user_firstName"));
				admin.setLastName(rs.getString("user_firstName"));
				admin.setMobileNum(rs.getString("user_mobileNum"));
				admin.setWebName(rs.getString("user_webName"));
				admin.setWebPwd(rs.getString("user_webPwd"));
			}
		});
		if(admin.getUserId()!=null&&admin.getUserId() != -1)
			return admin;
		return null;
	}
	/**
	 * 根据微信标识获取用户信息
	 * @param wxID
	 * @return
	 */
	public ZcUser getZcUser(String wxid) {
		ZcUser user=null;
		Object[] params = new Object[]{wxid};
		List<ZcUser> list = jdbcTemplate.query(Sql2zcConfig.queryZcUser, params, new ZcUser());
		if(list.size()>0){
			user=list.get(0);
		}
		return user;
	}
	
	
	/**
	 * 保持用户
	 * @param wxID
	 * @return
	 */
	public boolean saveZcUser(ZcUser e) {
		Object[] params = e.insertObjecParams();
		if(jdbcTemplate.update(Sql2zcConfig.insertZcUser, params) != 0)
			return true;
		return false;
	}
	
	/**
	 * 根据用户Id获取用户的OpenId（微信识别号）
	 * @param wxID
	 * @return
	 */
	public String getOpenIdByUserId(String userId) {
		final String[] OpenId={""};
		Object[] params = new Object[]{ userId };
		jdbcTemplate.query(Sql2zcConfig.queryZcUserOpenId, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				OpenId[0]=rs.getString("wxid");
			}
		});
		//如果查为空报错
		//String openId=jdbcTemplate.queryForObject(SqlConfig.queryUserOpenId, params, String.class);
		return OpenId[0];
	}
	
	
	/**
	 * 根据用户Id查找用户默认地址
	 * @param wxID
	 * @return
	 */
	public ZcUserAddr getZcUserAddr(Integer userId,Integer areaId) {
		ZcUserAddr userAddr=null;
		Object[] params = new Object[]{userId,areaId};
		List<ZcUserAddr> list = jdbcTemplate.query(Sql2zcConfig.queryZcUserAddr, params, new ZcUserAddr());
		if(list.size()>0){
			userAddr=list.get(0);
		}
		return userAddr;
	}
	
	/**
	 * 根据Id查找用地址
	 * @param wxID
	 * @return
	 */
	public ZcUserAddr queryZcUserAddrById(Integer id) {
		ZcUserAddr userAddr=null;
		Object[] params = new Object[]{id};
		List<ZcUserAddr> list = jdbcTemplate.query(Sql2zcConfig.queryZcUserAddrById, params, new ZcUserAddr());
		if(list.size()>0){
			userAddr=list.get(0);
		}
		return userAddr;
	}
	
	/**
	 * 根据用户Id查找用户默认地址
	 * @param wxID
	 * @return
	 */
	public List<ZcUserAddr> queryZcUserAddr(Integer userId,Integer areaId) {
		Object[] params = new Object[]{userId,areaId};
		List<ZcUserAddr> list = jdbcTemplate.query(Sql2zcConfig.queryZcUserAddrList, params, new ZcUserAddr());
		return list;
	}
	
	
	
	
	
	/**
	 * 保存用户地址
	 * @param wxID
	 * @return
	 */
	public boolean saveZcUserAddr(ZcUserAddr e) {
		Object[] params = e.insertObjecParams();
		if(jdbcTemplate.update(Sql2zcConfig.insertZcUserAddr, params) != 0)
			return true;
		return false;
	}
	
	/**
	 * 修改地址为默认地址
	 * @param wxID
	 * @return
	 */
	public boolean editZcUserAddrToDef(Integer id) {
		Object[] params =new Object[]{id};
		if(jdbcTemplate.update(Sql2zcConfig.editZcUserAddrDefById, params) != 0)
			return true;
		return false;
	}
	
	
	
	/**
	 * 修改用户地址
	 * @param wxID
	 * @return
	 */
	public boolean editZcUserAddr(ZcUserAddr e) {
		Object[] params = e.editObjecParams();
		if(jdbcTemplate.update(Sql2zcConfig.editZcUserAddr, params) != 0)
			return true;
		return false;
	}
	
	
	/**
	 * 删除用地址
	 * @param wxID
	 * @return
	 */
	public boolean delZcUserAddr(Integer id) {
		Object[] params = new Object[]{id};
		if(jdbcTemplate.update(Sql2zcConfig.delZcZcUserAddr, params) != 0)
			return true;
		return false;
	}
	
	
	/**
	 * 修改用户地址默认
	 * @param wxID
	 * @return
	 */
	public boolean editZcUserAddrDef() {
		if(jdbcTemplate.update(Sql2zcConfig.editZcUserAddrDef) != 0)
			return true;
		return false;
	}
	



}